package io.renren.modules.live.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.modules.live.entity.FanweUserEntity;
import io.renren.modules.live.service.FanweUserService;
import io.renren.modules.live.util.RabbitUtil;
import io.renren.modules.live.vo.FanweVideoPropVo;
import io.renren.modules.live.vo.getVideoProdMoneyByTimeAndIdDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.live.entity.FanweVideoPropEntity;
import io.renren.modules.live.service.FanweVideoPropService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 送礼物表
 *
 * @author shaoqi
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 09:18:22
 */
@Log4j2
@Transactional
@Api(tags = "送礼物管理")
@RestController
@RequestMapping("live/prop")
public class FanweVideoPropController {
    @Autowired
    private FanweVideoPropService fanweVideoPropService;
    @Autowired
    private RabbitUtil rabbitUtil;
    @Autowired
    private FanweUserService fanweUserService;



    @ApiOperation("获取直播间当天用户礼物金额信息")
    @GetMapping("/getVideoProdMoneyByTimeAndId")
    //@RequiresPermissions("live:fanweprop:info")
    public R getVideoProdMoneyByTime(@ApiParam(value = "直播间ID",required = true) Integer videoId){
        SimpleDateFormat format = new SimpleDateFormat();
        Date date = new Date(System.currentTimeMillis());
        String time = format.format(date);
        String s = time.substring(0, 8);
        BigDecimal decimal = new BigDecimal(0.00);
        //查询每个用户当天在直播间送礼的总金额
        List<getVideoProdMoneyByTimeAndIdDTO> userPropList = fanweUserService.getUserByIdSet(videoId,s);
        for (getVideoProdMoneyByTimeAndIdDTO dto : userPropList) {
            decimal = decimal.add(dto.getPropMoney());
        }
        return R.ok("获取成功").put("money",decimal).put("userPropList",userPropList);
    }

    @ApiOperation("用户送礼物给直播间主播")
    @PostMapping("/userGivingPorp")
    public R userGivingPorp(@RequestBody @ApiParam(value = "接受信息") FanweVideoPropVo fanweVideoPropVo){
        //先查寻用户余额是否小于送礼物总金额
        BigDecimal number = new BigDecimal(fanweVideoPropVo.getNum());
        BigDecimal decimal = number.multiply(fanweVideoPropVo.getDiamonds());

        FanweUserEntity UserEntity = fanweUserService.getById(fanweVideoPropVo.getUserId());
        BigDecimal bigDecimal = UserEntity.getMoney();

        BigDecimal comper = new BigDecimal(0.00);
        //比较大小返回的结果是int类型，-1表示小于，0是等于，1是大于
        if(decimal.compareTo(bigDecimal)== -1||decimal.compareTo(bigDecimal)==0){
            FanweVideoPropEntity propEntity = new FanweVideoPropEntity();
            propEntity.setPropMoney(decimal);
            propEntity.setUserId(fanweVideoPropVo.getUserId());
            propEntity.setCreateTime(new Date());
            propEntity.setPropId(fanweVideoPropVo.getPropId());
            propEntity.setAnchorId(fanweVideoPropVo.getAnchorId());
            propEntity.setNum(fanweVideoPropVo.getNum());
            propEntity.setVideoId(fanweVideoPropVo.getVideoId());
            propEntity.setPropName(fanweVideoPropVo.getPropName());
            propEntity.setDiamonds(fanweVideoPropVo.getDiamonds());
            //创建交换器和队列
//        rabbitUtil.addExchange(RabbitUtil.ExchangeType.DIRECT,"xiaoxi-order");
//        rabbitUtil.addQueue("exchange.order.bonus");
//        rabbitUtil.addExchange(RabbitUtil.ExchangeType.DIRECT,"xiaoxi-anchor");
//        rabbitUtil.addQueue("exchange.anchor.bonus");
//        rabbitUtil.addExchange(RabbitUtil.ExchangeType.DIRECT,"xiaoxi-user");
//        rabbitUtil.addQueue("exchange.user.bonus");

            fanweVideoPropService.save(propEntity);
//        HashMap<String,Object> hm = new HashMap<>();
//        hm.put("message",propEntity);
            try {
                //订单
                rabbitUtil.convertAndSend("xiaoxi.order", null, propEntity);
                //主播
                rabbitUtil.convertAndSend("xiaoxi.anchor", null, propEntity);
                //用户
                rabbitUtil.convertAndSend("xiaoxi.usr", null, propEntity);
                return R.ok("送礼成功");
            }catch (Exception e){
                log.info(e);
                return R.error("送礼失败");
            }
        }else {
            return R.error("您的可用余额不足,请充值");
        }
    }


    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("live:fanwevideoprop:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fanweVideoPropService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("live:fanwevideoprop:info")
    public R info(@PathVariable("id") Integer id){
		FanweVideoPropEntity fanweVideoProp = fanweVideoPropService.getById(id);

        return R.ok().put("fanweVideoProp", fanweVideoProp);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("live:fanwevideoprop:save")
    public R save(@RequestBody FanweVideoPropEntity fanweVideoProp){
		fanweVideoPropService.save(fanweVideoProp);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("live:fanwevideoprop:update")
    public R update(@RequestBody FanweVideoPropEntity fanweVideoProp){
		fanweVideoPropService.updateById(fanweVideoProp);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("live:fanwevideoprop:delete")
    public R delete(@RequestBody Integer[] ids){
		fanweVideoPropService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
